/**
 * 线性查找法
 *
 * @author jspankai@vip.qq.com
 * @date 2021/4/28 0:09
 * @className LinearSearch
 */
public class LinearSearch {
    /**
     * private 用户就不能创建LinearSearch这个对象了
     */
    private LinearSearch() {
    }

    public static <E> int search(E[] data, E target) {
        for (int i = 0; i < data.length; i++)
            // 由于使用了泛型，此时data[i],已经不是基本数据类型了，变成了一个类对象，target也是类对象，判断相等用equals
            if (data[i].equals(target))
                return i;
        return -1;
    }

    public static void main(String[] args) {
        int[] dataSize = {1000000, 10000000};
        for (int n : dataSize) {
            Integer[] data = ArrayGenerator.generateOrderedArray(n);

            long startTime = System.nanoTime();

            for (int k = 0; k < 100; k++)
                search(data, n);

            long endTime = System.nanoTime();
            double time = (endTime - startTime) / 1000000000.0;

            System.out.println("n= " + n + ", 100 runs : " + time + " s");
        }


//        int search1 = search(data, 666);
//        System.out.println(search1);
//
//        Student[] students = {new Student("Alice"),
//                new Student("Bobo"),
//                new Student("Charles")};
//        Student bobo = new Student("bobo");
//        int search2 = search(students, bobo);
//        System.out.println(search2);
    }
}
